<?php
class Administrator_user_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }


	public function ajax_valid_user()
	{
		$username = $this->input->post('username');
		$is_exist = $this->db->from('user')
		->where(array('username' => $username))
		->count_all_results();
		if(!$is_exist) echo "valid";
	}
	
	public function ajax_valid_email()
	{
		$email = $this->input->post('email');
		$username = $this->input->post('username');
		$is_exist = $this->db->from('user')
		->where(array('email' => $email,'username !=' => $username))
		->count_all_results();
		if(!$is_exist) echo "valid";
	}
			
	public function user_profile()
	{
		$email = $this->input->post('email');
		$first_name = $this->input->post('first_name');
		$last_name = $this->input->post('last_name');
		$nickname = $this->input->post('nickname');
		$telephone = $this->input->post('telephone');
		$password = $this->input->post('pass1');
		
		/*Update User*/
		$data = array(
			'email' => $email
		);
		if(!empty($password)) $data['password'] = $password;
		$this->db->update('user',$data,array('user_id' => $this->session->userdata('adminData')['user_id']));
		
		/*Update User Info*/
		$data = array(
			'first_name' => $first_name,
			'last_name' => $last_name,
			'nickname' => $nickname,
			'telephone' => $telephone
		);	
		$this->db->update('user_info',$data,array('user_id' => $this->session->userdata('adminData')['user_id']));
	}
	
	public function user_management_add()
	{
		$is_public = $this->input->post('is_public');
		$group_id = $this->input->post('group_id');
		$username = $this->input->post('user_name');
		$password = $this->input->post('pass1');
		$first_name = $this->input->post('first_name');
		$last_name = $this->input->post('last_name');
		$nickname = $this->input->post('nickname');
		$user_email = $this->input->post('user_email');
		$telephone = $this->input->post('telephone');
		$datetime = date('Y-m-d H:i:s');
		
		/*Insert user*/
		$data = array(
			'group_id' => $group_id,
			'username' => $username,
			'password' => $password,
			'email' => $user_email,
			'is_public' => ''.$is_public.'',
			'create_date' => $datetime,
			'update_Date' => $datetime
		);
		$this->db->insert('user',$data);
		$user_id = $this->db->insert_id();
		
		/*Insert user_info*/
		$data = array(
			'user_id' => $user_id,
			'first_name' => $first_name,
			'last_name' => $last_name,
			'nickname' => $nickname,
			'telephone' => $telephone
		);
		$this->db->insert('user_info',$data);
	}	
}

?>